<!-- 信谊下发移库指令编辑弹窗 -->
<template>
  <div>
    <el-dialog
      :title="isUpdate?'更新事业部':'更新事业部'"
      :visible="visible"
      width="40%"
      :destroy-on-close="true"
      :lock-scroll="false"
      @update:visible="updateVisible">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="82px">
        <el-row>
          <el-col :lg="12" :md="12">
            <el-form-item label="事业部:" label-width="92px" prop="companycode">
            <el-select size="mini"
                       clearable
                       v-model="form.companycode"
                       placeholder="请选择板块"
                       class="ele-fluid">
              <el-option v-for="item in ssbkList" :label="item.name" :value="item.code" :key="item.code" />
            </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="24" :md="24">
            <el-form-item label="NC批次号:" label-width="92px" prop="ncbatch">
              <el-input
                clearable
                :maxlength="50"
                v-model="form.ncbatch"
                placeholder=""/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item v-if="false" label="备注 :" label-width="92px">
          <el-input
            :rows="2"
            :disabled="form.status=='已反馈'"
            clearable
            type="textarea"
            :maxlength="200"
            v-model="form.feedback"
            placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="updateVisible(false)">取消</el-button>
        <el-button
          type="primary"
          @click="save"
          :disabled="form.status=='已反馈'"
          :loading="loading">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: 'KdlSineFeedbackEdit',
  // components: {SelectInv},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      url:'',
      itemList:[],
      ssbkList:[],
      bincodeList:[],
      item:{
        itemcode:'',
        batch:'',
        plant:'',
        uom:'',
        qty:'',
        boxflag:'',
        boxno:''
      },
      columns:[
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'itemcode',
          label: '物料号',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'batch',
          label: '批号',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'center'
        },
        {
          prop: 'qty',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot:'qty'
        },
        {
          prop: 'uom',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'plant',
          label: '工厂',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'boxflag',
          label: '箱型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'boxno',
          label: '箱号',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
      ],
      current: null,
      // 表单数据
      form: {
        orderdate: new Date(),
        ordertype: 'ZBFE',
      },
      // 表单验证规则
      rules: {
        companycode: [
          {required: true, message: '请选择事业部', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      showSelectInv:false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.getBincodeList();
        this.isUpdate = true;
      } else {
        this.form = {};
        this.initForm();
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    this.$nextTick(()=>{
      this.getSsbkList();
    });
  },
  methods: {
    /* 获取所属版块 */
    async getSsbkList(){
      var res = await this.$http.get('/dictdata/index?dictId=19&page=1&limit=1000');
      if(res.status==200)
      {
        this.ssbkList = res.data.data.records;
      }
    },
    /**
     * 获取仓库列表
     */
    getBincodeList() {
      if(this.data) {
        this.$http.get('/kdl/bincodelist/' + this.data.plant).then(res => {
          this.bincodeList = res.data.data;
        }).catch(e => {
          this.$message.error(e.message);
        });
      }
    },
    initForm(){
      this.form.ordertype='ZBFE';
      this.form.orderdate = new Date();
    },
    addItem(){
      //let newitem = Object.assign({},this.item);
      //this.itemList.push(newitem);
      this.showSelectInv=true;
    },
    removeItem(){},
    loadInv(list){
      for(let i=0;i<list.length;i++)
      {
        let newitem = Object.assign({},this.item);
        newitem.itemcode = list[i].invcode;
        newitem.batch = list[i].vlotno;
        newitem.qty  = list[i].nonhandnum;
        newitem.boxflag = list[i].boxflag;
        newitem.boxno = list[i].boxno;
        this.itemList.push(newitem);

      }
    },

    /* 保存编辑 */
    save() {
      // if(!this.form.tolocation){
      //   this.$message.info('请选择转入库位');
      //   return;
      // }
      // if(!this.form.result){
      //   this.$message.info('请输入处理意见');
      //   return;
      // }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'post' : 'post'](this.isUpdate ? '/kdlgm/savefeedback' : '/kdlgm/savefeedback', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
